package com;

import com.aop.SpElAop.DistributedLock.RedisLock.Test.LockDTO;
import com.aop.SpElAop.DistributedLock.RedisLock.Test.TestLockService;
import com.aop.SpElAop.LogAop.Test.BaseInfo;
import com.aop.SpElAop.LogAop.Test.InnerList;
import com.aop.SpElAop.LogAop.Test.LogTestService;
import com.aop.SpElAop.LogAop.Test.Person;
import com.batchOperation.Template.TemplateService;
import com.batchOperation.Template.model.BatchOperationTemplate;
import com.batchOperation.Template.model.BatchOperationTemplateContent;
import com.batchOperation.boot.BatchOperatorBootService;
import com.batchOperation.excel.executor.ExecutorInfo;
import com.batchOperation.excel.model.Employee;
import com.batchOperation.excel.model.SchoolInfo;
import com.workflowengine.WorkFlowService;
import com.workflowengine.config.NodeConfig;
import com.workflowengine.config.WorkFlowConfig;
import com.workflowengine.config.WorkFlowContent;
import com.workflowengine.event.workflowevent.WorkFlowEventContent;
import com.workflowengine.event.workflowevent.events.NodeEndEvent;
import com.workflowengine.event.workflowevent.events.NodeStartEvent;
import com.workflowengine.event.workflowevent.events.WorkFlowStartEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com
 * @ClassName: MyspringbootApplication
 * @Author: h2cloud
 * @CreateTime: 2024/7/8 11:33 下午
 * @Description:
 */
@SpringBootApplication
@Configuration
@EnableAsync
public class MyspringbootApplication {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(MyspringbootApplication.class, args);
        // testLock(applicationContext);
        //  testTemplateDownload(applicationContext);
      parseFile(applicationContext);
//         testLog(applicationContext);
    }

    public static void parseFile(ConfigurableApplicationContext applicationContext) {
        BatchOperatorBootService batchOperatorBootService = applicationContext.getBean(BatchOperatorBootService.class);
        ExecutorInfo executorInfo = new ExecutorInfo();
        executorInfo.setTemplateId(1024L);
        executorInfo.setFileName("template-测试名称1735664125346.xlsx");
        executorInfo.setMainObj(Employee.class.getSimpleName());
        batchOperatorBootService.parseFile(executorInfo);
    }

    public static void testTemplateDownload(ConfigurableApplicationContext applicationContext) {
        TemplateService templateService = applicationContext.getBean(TemplateService.class);

        BatchOperationTemplate batchOperationTemplate = new BatchOperationTemplate();
        batchOperationTemplate.setTemplateId(1024L);
        batchOperationTemplate.setTemplateName("测试名称");
        batchOperationTemplate.setContents(new ArrayList<>());
        batchOperationTemplate.setEntityList(new ArrayList<>());

        List<BatchOperationTemplateContent> contents = batchOperationTemplate.getContents();

        batchOperationTemplate.getEntityList().add(Employee.class.getSimpleName());
        for (Field declaredField : Employee.class.getDeclaredFields()) {
            if (declaredField.getName().equals("userRank"))
                continue;
            BatchOperationTemplateContent content = new BatchOperationTemplateContent();
            content.setEntity(Employee.class.getSimpleName());
            content.setFieldId(declaredField.getName());
            contents.add(content);
        }

        batchOperationTemplate.getEntityList().add(SchoolInfo.class.getSimpleName());
        for (Field declaredField : SchoolInfo.class.getDeclaredFields()) {
            if (declaredField.getName().equals("userRank"))
                continue;
            BatchOperationTemplateContent content = new BatchOperationTemplateContent();
            content.setEntity(SchoolInfo.class.getSimpleName());
            content.setFieldId(declaredField.getName());
            contents.add(content);
        }

        templateService.createOrModifyTemplate(batchOperationTemplate);
        System.out.println("end");
    }


    public static void testWorkFlow(ConfigurableApplicationContext applicationContext) {
        WorkFlowConfig workFlowConfig = new WorkFlowConfig();

        WorkFlowEventContent workFlowEventContent = new WorkFlowEventContent();
        workFlowEventContent.setEmailRule("1");
        workFlowEventContent.setLetterRule("2");

        WorkFlowStartEvent workFlowStartEvent = new WorkFlowStartEvent(workFlowEventContent);
        workFlowConfig.setWorkFlowStartEvent(workFlowStartEvent);

        List<NodeConfig> nodeConfigList = new ArrayList<>();
        NodeConfig nodeConfig = new NodeConfig();
        NodeStartEvent nodeStartEvent = new NodeStartEvent(workFlowEventContent);
        nodeConfig.setNodeStartEvent(nodeStartEvent);
        NodeEndEvent nodeEndEvent = new NodeEndEvent(workFlowEventContent);
        nodeConfig.setNodeEndEvent(nodeEndEvent);
        nodeConfigList.add(nodeConfig);
        workFlowConfig.setNodeConfigList(nodeConfigList);

        WorkFlowContent workFlowContent = new WorkFlowContent();
        workFlowContent.setWorkFlowConfig(workFlowConfig);

        WorkFlowService workFlowService = applicationContext.getBean(WorkFlowService.class);
        workFlowService.startWorkFlow(workFlowContent);
        workFlowService.executeWorkFlow(workFlowContent);
    }

    public static void testLog(ConfigurableApplicationContext applicationContext) {
        final LogTestService bean = applicationContext.getBean(LogTestService.class);
        bean.test1("hhhh");
//		bean.test2();

        BaseInfo baseInfo = new BaseInfo("广东大学");
        List<InnerList> lists = new ArrayList<>();
        InnerList innerList = new InnerList("内部类");
        lists.add(innerList);
        Person p = new Person("longkai", 10, baseInfo, lists);
        bean.test3(p);
        bean.test4(p);
        bean.test4(p);

        bean.test5(p);
    }

    public static void testLock(ConfigurableApplicationContext applicationContext) {
        TestLockService bean = applicationContext.getBean(TestLockService.class);
        LockDTO lockDTO = new LockDTO();
        lockDTO.setId("1024");
        bean.single(lockDTO);
        bean.single(lockDTO);
    }
}
